package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Currency;
import org.hgvc.webapp.ets.bo.CurrencyRate;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class CurrencyRateBean extends AbstractBean{
	public static final String BEAN_NAME="currencyRateBean";
	
	private MasterSetupService masterSetupService;
	private List<SelectItem> currencySelectList;
	private List<Currency> currencyList;
	private List<CurrencyRate> currencyRateList;
	private Currency currency;
	private CurrencyRate currencyRate;
	private String currencyId;
	private boolean catchError;
	
	public List<CurrencyRate> getCurrencyRateList() {
		try{
			if(currencyId!=null&&currencyId.length()!=0){
				currencyRateList = masterSetupService.getCurrencyRateListByCurrencyId(currencyId);
			}else{
				currencyRateList = new ArrayList<CurrencyRate>();
			}	
		}catch(BaseException be){
			initErrorBean(be);
		}
		return currencyRateList;
	}
	public CurrencyRate getCurrencyRate() {
		return currencyRate;
	}
	public void setCurrencyRate(CurrencyRate currencyRate) {
		this.currencyRate = currencyRate;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	public List<SelectItem> getCurrencySelectList() {
		return currencySelectList;
	}
	
	public void init(){
		try{
			this.currencyList = masterSetupService.getCurrencyList();
			currencySelectList = new ArrayList<SelectItem>();
			for (Currency currency : currencyList){
				SelectItem selectItem = new SelectItem(currency.getCurrencyId(),currency.getDesc());
				currencySelectList.add(selectItem);
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public Currency getCurrency(){
		
		if(currencyId!=null&&currencyId.length()!=0&&currencyList!=null&&currencyList.size()>0){
			for(Currency currency:this.currencyList){
				if(currency.getCurrencyId().equalsIgnoreCase(this.currencyId)){
					this.currency = currency;
					break;
				}
			}
		}else
			this.currency=new Currency();
		
		return this.currency;
	}
	public String getCurrencyId() {
		return currencyId;
	}
	public void setCurrencyId(String currencyId) {
		this.currencyId = currencyId;
	}
	
	public String navAddAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		this.currencyRate=new CurrencyRate();
		return outcome;
	}
	public String addAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.currencyRate.setCreatedBy(getThisUser());
			this.currencyRate.setCreatedDate(new Date());
			this.currencyRate.setIsDeleted(CommonConstant.NOT_DELETED);
			this.currencyRate.setCurrency(getCurrency());
			this.masterSetupService.saveCurrencyRate(currencyRate);
			this.currencyRateList = this.masterSetupService.getCurrencyRateListByCurrencyId(currencyId);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void navEditActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("currencyRateId");
		String currencyRateId = (String)param.getValue();
		for(CurrencyRate currencyRate:this.currencyRateList){
			if(currencyRateId.equalsIgnoreCase(currencyRate.getCurrencyRateId())){
				this.currencyRate = currencyRate;
				break;
			}
		}
	}
	
	public String updateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.currencyRate.setModifiedBy(getThisUser());
			this.currencyRate.setModifiedDate(new Date());
			this.masterSetupService.updateCurrencyRate(this.currencyRate);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<CurrencyRate> deleteList = new ArrayList<CurrencyRate>();
			for (CurrencyRate currencyRate : this.currencyRateList){
				if (currencyRate.isMarkAsAction())
					deleteList.add(currencyRate);
			}
			this.masterSetupService.deleteCurrencyRates(deleteList);
			this.currencyRateList=this.masterSetupService.getCurrencyRateListByCurrencyId(currencyId);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		return outcome;
	}
}
